package cn.tedu.bailianagent.controller;

import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//非流式调用
@RestController
@RequestMapping("/ai")
@Slf4j
public class BaiLianAgentController {
    //DashScopeAgent对象，用于与百炼进行交互
    private DashScopeAgent agent;
    @Value("${spring.ai.dashscope.agent.options.app-id}")
    private String appId;

    public BaiLianAgentController(DashScopeAgentApi dashScopeAgentApi) {
        this.agent = new DashScopeAgent(dashScopeAgentApi);
    }
    @GetMapping("/bailian/agent/call")
    public String call(String message){
        log.debug("调用百炼大模型：message:{}",message);
        log.debug("appId:{}",appId);
        ChatResponse response = agent.call(new Prompt(message,
                DashScopeAgentOptions.builder().withAppId(appId).build()));
        log.debug("百炼大模型返回结果response:{}",response);
        String content=response.getResult().getOutput().getText();
        log.debug("返回结果中提取文本：content:{}",content);
        return content;

    }

}
